package com.handinglian.task.controller;

import cn.afterturn.easypoi.entity.vo.NormalExcelConstants;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.afterturn.easypoi.view.PoiBaseView;
import com.apidoc.annotation.Api;
import com.apidoc.annotation.ApiVersion;
import com.handinglian.common.dto.CommonPage;
import com.handinglian.common.dto.ResultCode;
import com.handinglian.common.dto.ResultData;
import com.handinglian.common.factory.ResultDataFactory;
import com.handinglian.common.shiro.Principal;
import com.handinglian.task.dto.*;
import com.handinglian.task.param.*;
import com.handinglian.task.service.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@Api("任务列表")
@RestController
@Slf4j
@RequestMapping("/{version}/task")
@ApiVersion(1.0)
public class TaskController {
    @Autowired
    private TaskInfoService taskInfoService;
    @Autowired
    private TaskUserCallService taskUserCallService;
    @Autowired
    private TaskUserMessageService taskUserMessageService;
    @Autowired
    private TaskUserNoteService taskUserNoteService;
    @Autowired
    private TaskUserRelationService taskUserRelationService;
    
    /**
     * 创建任务列表
     */
    @RequiresPermissions("task_list:create")
    @PostMapping("/createTaskInfo")
    public ResultData createTaskInfo(@RequestBody TaskInfoCreateParam taskInfoCreateParam, Principal principal){
        Integer id = taskInfoService.createTaskInfo(taskInfoCreateParam, principal.getUserId());
        return ResultDataFactory.generateResultData(id);
    }

    /**
     * 删除任务列表
     */
    @RequiresPermissions("task_list:delete")
    @DeleteMapping(value = "/deleteTaskInfo")
    public ResultData deleteTaskInfo(Integer taskId) {
        int amount = taskInfoService.deleteTaskInfo(taskId);
        return ResultDataFactory.generateResultData(amount);
    }

    /**
     * 删除任务分配
     */
    @RequiresPermissions("task_list:delete")
    @DeleteMapping(value = "/deleteTaskAssign")
    public ResultData deleteTaskAssign(Integer taskUserId) {
        int amount = taskUserRelationService.deleteTaskUserByTaskId(taskUserId);
        return ResultDataFactory.generateResultData(amount);
    }

    /**
     * 更新任务列表
     */
    @RequiresPermissions("task_list:update")
    @PutMapping(value = "/updateTaskInfo")
    public ResultData updateTaskInfo(@RequestBody TaskInfoUpdateParam taskInfoUpdateParam) {
        int amount = taskInfoService.updateTaskInfo(taskInfoUpdateParam);
        return ResultDataFactory.generateResultData(amount);
    }


    /**
     * 查询任务列表详情
     */
    @RequiresPermissions("task_list:list")
    @GetMapping(value = "/loadTaskInfoDetail")
    public ResultData<TaskInfoDetailDto> loadTaskInfoDetail(Integer taskId) {
        TaskInfoDetailDto taskInfoDetailDto = taskInfoService.loadTaskInfoDetail(taskId);
        return ResultDataFactory.generateSuccessResultData(taskInfoDetailDto);
    }

    /**
     * 查询任务列表详情(APP)
     */
    @RequiresPermissions("task_list:list")
    @GetMapping(value = "/getDetailApp")
    public ResultData<TaskInfoDetailDto> getDetailApp(Principal principal, Integer taskId) {
        TaskInfoDetailDto taskInfoDetailDto = taskInfoService.getDetailApp(principal.getUserId(),taskId);
        return ResultDataFactory.generateSuccessResultData(taskInfoDetailDto);
    }

    /**
     * 获取任务列表
     */
    @RequiresPermissions("task_list:list")
    @GetMapping("/inquireTaskInfoPageList")
    public ResultData<CommonPage<TaskInfoDto>> inquireTaskInfoPageList(Principal principal, String titleOrDesc, String startDate, String endDate, String sortField, String sortOrder, String taskState, Integer pageIndex, Integer pageSize) {
        CommonPage<TaskInfoDto> taskInfoPageList = taskInfoService.inquireTaskInfoPageList(principal.getUserId(), titleOrDesc, startDate, endDate, sortField, sortOrder, taskState, pageIndex, pageSize);
        return ResultDataFactory.generateSuccessResultData(taskInfoPageList);
    }

    /**
     * 是否有待呼任务
     */
    @RequiresPermissions("task_list:list")
    @GetMapping("/hasTask")
    public ResultData hasTask(Integer userId) {
        boolean flag = taskInfoService.hasTask(userId);
        if (flag){
            return new ResultData(ResultCode.SUCCESS.getCode(), "存在待呼任务", flag);
        } else {
            return new ResultData(ResultCode.SUCCESS.getCode(), "无待呼任务", flag);
        }
    }

    /**
     * 获取任务列表(APP)
     */
    @RequiresPermissions("task_list:list")
    @GetMapping("/inquireTaskInfoListByTaskState")
    public ResultData<TaskInfoDto> inquireTaskInfoListByTaskState(Principal principal, Integer taskState, String taskTitle) {
        List<TaskInfoDto> taskInfoDtos = taskInfoService.inquireTaskInfoListByTaskState(taskState, principal.getUserId(), taskTitle);
        return ResultDataFactory.generateSuccessResultData(taskInfoDtos);
    }

    /**
     * 获取分配信息列表
     */
    @RequiresPermissions("task_list:list")
    @GetMapping("/inquireAssignPageList")
    public ResultData<CommonPage<AssignDto>> inquireAssignPageList(Integer taskId, String customerNameOrPhone, String userNameOrJobNum, String sortField, String sortOrder, String phoneStatus, String customerIntentions, Integer valid, Integer pageIndex, Integer pageSize) {
        CommonPage<AssignDto> pageInfo = taskInfoService.inquireAssignPageList(taskId, customerNameOrPhone, userNameOrJobNum, sortField, sortOrder, phoneStatus, customerIntentions, valid, pageIndex, pageSize);
        return ResultDataFactory.generateSuccessResultData(pageInfo);
    }

    /**
     * 获取分配信息列表(APP)
     */
    @RequiresPermissions("task_list:list")
    @GetMapping("/inquireAssignAppList")
    public ResultData<AssignAppDto> inquireAssignAppList(Principal principal, Integer taskId, String customerNameOrPhone, Integer callState, Integer userTagId) {
        List<AssignAppDto> assignAppDtos = taskInfoService.inquireAssignAppList(principal.getUserId(), taskId, customerNameOrPhone, callState, userTagId);
        return ResultDataFactory.generateSuccessResultData(assignAppDtos);
    }

    /**
     * 获取分配信息详情
     */
    @RequiresPermissions("task_list:list")
    @GetMapping("/loadAssignDetail")
    public ResultData<AssignDetailDto> loadAssignDetail(Integer taskUserId) {
        List<AssignDetailDto> assignDetailDtoList = taskInfoService.loadAssignDetail(taskUserId);
        return ResultDataFactory.generateSuccessResultData(assignDetailDtoList);
    }

    /**
     * 获取分配信息详情(APP)
     */
    @RequiresPermissions("task_list:list")
    @GetMapping("/loadAssignDetailApp")
    public ResultData<AssignDetailAppDto> loadAssignDetailApp(Principal principal, Integer taskUserId) {
        AssignDetailAppDto assignDetailAppDto = taskInfoService.loadAssignAppDetail(principal.getUserId(), taskUserId);
        return ResultDataFactory.generateSuccessResultData(assignDetailAppDto);
    }

    /**
     * 任务信息模板导出
     */
    @RequiresPermissions("task_list:list")
    @GetMapping("/exportTaskTemplateExcel")
    public void exportTaskTemplateExcel(ModelMap map, HttpServletRequest request, HttpServletResponse response) {
        ExportParams params = new ExportParams("分配信息(每个客户只能出现一次)", "分配信息", ExcelType.XSSF);
        map.put(NormalExcelConstants.DATA_LIST, new ArrayList<>());
        map.put(NormalExcelConstants.CLASS, TaskTemplateExcel.class);
        map.put(NormalExcelConstants.PARAMS, params);
        map.put(NormalExcelConstants.FILE_NAME, "分配任务");
        PoiBaseView.render(map, request, response, NormalExcelConstants.EASYPOI_EXCEL_VIEW);
    }

    /**
     * 导出分配信息
     */
    @RequiresPermissions("task_list:list")
    @GetMapping("/exportAssignInfo")
    public void exportAssignInfo(Integer taskId,  ModelMap map, HttpServletRequest request, HttpServletResponse response) {
        taskInfoService.exportAssignInfo(taskId, map, request, response);
    }

    /**
     * 任务分配信息导入
     */
    @RequiresPermissions("task_list:assign")
    @PostMapping("/importTaskTemplateExcel")
    public ResultData<ImportTaskDto> importTaskTemplateExcel(Integer taskId, ModelMap map, HttpServletRequest request, HttpServletResponse response, MultipartFile file, Integer flag) throws Exception {
        ImportTaskDto importTaskDto = taskInfoService.importTaskTemplateExcel(taskId, map, request, response, file, flag);
        return ResultDataFactory.generateSuccessResultData(importTaskDto);
    }

    /**
     * 创建任务呼叫信息(APP)
     */
    @PostMapping("/createTaskUserCall")
    public ResultData createTaskUserCall(@RequestBody List<Integer> taskUserIds) throws Exception {
        int amount = taskUserCallService.createTaskUserCall(taskUserIds);
        return ResultDataFactory.generateResultData(amount);
    }

    /**
     * 创建任务短信信息(APP)
     */
    @PostMapping("/createTaskUserMessage")
    public ResultData createTaskUserMessage(@RequestBody TaskUserMessageCreateParam taskUserMessageCreateParam) throws Exception {
        int amount = taskUserMessageService.createTaskUserMessage(taskUserMessageCreateParam);
        return ResultDataFactory.generateResultData(amount);
    }

    /**
     * 创建任务笔记信息(APP)
     */
    @PostMapping("/createTaskUserNote")
    public ResultData createTaskUserNote(@RequestBody TaskUserNoteCreateParam taskUserNoteCreateParam) throws Exception {
        int amount = taskUserNoteService.createTaskUserNote(taskUserNoteCreateParam);
        return ResultDataFactory.generateResultData(amount);
    }

    /**
     * 更新客户意向(APP)
     */
    @PostMapping("/updateCustomerIntention")
    public ResultData updateCustomerIntention(@RequestBody List<CustomerIntentionUpdateParam> customerIntentionUpdateParams) throws Exception {
        int amount = taskUserRelationService.updateCustomerIntention(customerIntentionUpdateParams);
        return ResultDataFactory.generateResultData(amount);
    }

    /**
     * 更新分配信息
     */
    @RequiresPermissions("task_list:update")
    @PostMapping("/updateTaskUserRelation")
    public ResultData updateTaskUserRelation(@RequestBody TaskUserRelationUpdateParam taskUserRelationUpdateParam) throws Exception {
        int amount = taskUserRelationService.updateTaskUserRelation(taskUserRelationUpdateParam);
        return ResultDataFactory.generateResultData(amount);
    }

    /**
     * 置顶分配信息(APP)
     */
    @RequiresPermissions("task_list:update")
    @PutMapping("/updateTaskUserTop")
    public ResultData updateTaskUserTop(@RequestBody TaskUserTopParam taskUserTopParam) {
        int amount = taskUserRelationService.updateTop(taskUserTopParam);
        return ResultDataFactory.generateResultData(amount);
    }
}
